package io.jplus.admin.service.impl;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import io.jboot.aop.annotation.Bean;
import io.jboot.core.rpc.annotation.JbootrpcService;
import io.jboot.db.model.Column;
import io.jboot.db.model.Columns;
import io.jboot.service.JbootServiceBase;
import io.jplus.JplusConstants;
import io.jplus.admin.model.Menu;
import io.jplus.admin.service.MenuService;
import io.jplus.admin.service.RoleMenuService;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;

@Bean
@Singleton
@JbootrpcService
public class MenuServiceImpl extends JbootServiceBase<Menu> implements MenuService {

    @Inject
    RoleMenuService roleMenuService;

    @Override
    public List<Menu> findByColumns(Columns columns) {
        List<Menu> menuList = DAO.findListByColumns(columns);
        for (Menu menu : menuList) {
            Menu parent = findById(menu.getParentId());
            if (parent != null) {
                menu.setParentName(parent.getName());
            }
        }
        return DAO.findListByColumns(columns);
    }

    @Override
    public List<Menu> findListByUserId(Long userId) {
        //系统管理员，拥有最高权限
        if (userId == JplusConstants.SUPER_ADMIN) {
            return getAllMenuList(null);
        }
        List<Long> menuIdList = roleMenuService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public List<Menu> findListParentId(Long parentId, List<Long> menuIdList) {
        List<Menu> menuList = findListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        List<Menu> userMenuList = new ArrayList<>();
        for (Menu menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<Menu> findNotButtonList() {
        SqlPara sqlPara = Db.getSqlPara("admin-menu.queryNotButtonList");
        return DAO.find(sqlPara);
    }

    @Override
    public Menu findMenuById(Object id) {
        Menu menu = findById(id);
        Menu parent = findById(menu.getParentId());
        if (parent != null) {
            menu.setParentName(parent.getName());
        }
        return menu;
    }

    @Override
    public List<Menu> findListParentId(Long parentId) {
        return DAO.findListByColumn(Column.create("parent_id", parentId));
    }

    private List<Menu> getAllMenuList(List<Long> menuIdList) {
        //查询根菜单列表
        List<Menu> menuList = findListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);
        return menuList;
    }

    private List<Menu> getMenuTreeList(List<Menu> menuList, List<Long> menuIdList) {
        List<Menu> subMenuList = new ArrayList<Menu>();

        for (Menu entity : menuList) {
            //目录
            if (entity.getType() == JplusConstants.MenuType.CATALOG.getValue()) {
                entity.setList(getMenuTreeList(findListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }

    @Override
    public boolean deleteById(Object... ids) {
        boolean tag = false;
        for (Object id : ids) {
            tag = DAO.deleteById(id);
        }
        return tag;
    }
}